﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
   public  class AdminUserDAO
    {
       public void Add(AdminUser adminUser)
       {
           DBHelper.TransSaveOrUpdate(adminUser);
       }
       public void DeleteUserById(int id)
       {
           DBHelper.TransDeleteByID(" AdminUser ",id);
       }
       public void ModifyAdminUser(AdminUser adminUser)
       {
           DBHelper.TransSaveOrUpdate(adminUser);
       }
       public IList<AdminUserDAO> GetAllAdminUser()
       {
          return  DBHelper.ExecuteHQL("from AdminUser ").List<AdminUserDAO>();
       }

       public Com.CMS263.PO.AdminUser Login(string userName, string password)
       {
           IList<AdminUser> list = DBHelper.ExecuteHQL("from AdminUser where userName='" + userName + "' and password='" + password + "'").List<AdminUser>();
           if (list.Count > 0)
           {
               return list.First();
           }
           else
           {
               return null;
           }
       }

       public IList<UserList> GetUserList()
       {
           IList<UserList> userList = new List<UserList>();
           IList<object> list = DBHelper.ExecuteHQL("select a.id,a.userName,r.roleName from AdminUser as a,AdminRole as r where a.roleID=r.id order by a.roleID asc ").List<object>();
           foreach (object obj in list)
           {
               UserList ul = new UserList();
               Object[] objs = (Object[])obj;

               ul.id = (int)objs[0];
               ul.roleName = (string)objs[2];
               ul.userName = (string)objs[1];
            //   ul.UserEmail = (string)objs[3];
               userList.Add(ul);
           }
           return userList;
       }

       public bool isExist(string userName)
       {
           return DBHelper.ExecuteHQL("from AdminUser where userName='"+userName+"'").List().Count>0;
       }

       public AdminUser GetAdminUserByID(int id)
       {
           return (AdminUser)DBHelper.GetObject(typeof(AdminUser),id);
       }
    }
}
